package com.zkx.mvp.persenter;

import android.text.TextUtils;

import com.zkx.mvp.api.HttpApi;
import com.zkx.mvp.contract.TestContract;
import com.zkx.mvp.model.test.TestResponse;
import com.zkx.mvp.persenter.support.BaseSubscriber;
import com.zkx.mvp.utils.LogUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Author：zhangkx on 2017/6/28 12:04
 * Email：zkx9025@163.com
 */

public class TestPresenter<T extends TestContract.View> implements TestContract.Presenter<T>{
    private T mView;
    private HttpApi httpApi;

    public TestPresenter() {
        httpApi = HttpApi.getInstance();
    }

    @Override
    public void test(String city) {
        if (TextUtils.isEmpty(city)) {
            mView.onFailed("请输入你要查询的城市");
            return;
        }
        httpApi.getWeather(city)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<TestResponse>(){
                    @Override
                    public void onNext(TestResponse response) {
                        super.onNext(response);
                        mView.onResponse(response.toString());
                        LogUtils.i("TestPresenter","response :" +response.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onResponse(e.toString());
//                        mView.onFailed("异常 ："+e.toString());
                    }
                });
    }

    @Override
    public void attachView(T view) {
        mView = view;
    }

    @Override
    public void detachView() {

    }
}
